﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Microsoft.AspNetCore.Mvc;
using shenghua.Authorization;
using shenghua.Commodity.Commodities;
using shenghua.Commodity.Commodities.Dto;
using shenghua.Common.Dto;
using shenghua.Member.Collection.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Member.Collection
{
    public class CollectionAppService: shenghuaAppServiceBase, ICollectionAppService
    {
        private readonly CollectionManager _collectionManager;
        private readonly IRepository<CollectionCommodity> _collectionCommodityRepository;
        private readonly CommodityManager _commodityManager;

        public CollectionAppService(
            CollectionManager collectionManager,
            IRepository<CollectionCommodity> collectionCommodityRepository,
            CommodityManager commodityManager
            )
        {
            _collectionManager = collectionManager;
            _collectionCommodityRepository = collectionCommodityRepository;
            _commodityManager = commodityManager;
        }

        [AbpAuthorize(PermissionNames.Member)]
        public async Task<CollectionCommodityDto> Add(AddCollectionItemDto input)
        {
            var member = await GetCurrentMemberAsync();
            var realCommodity = await _commodityManager.GetRealCommodity(member, input.CommodityId, input.ShoppingOptionId);

            CollectionCommodity collectionCommodity = new CollectionCommodity
            {
                CommodityId = input.CommodityId,
                ShoppingOptionId = input.ShoppingOptionId,
                CommodityName = realCommodity.CommodityName,
                CommodityPicture = realCommodity.Picture,
                CommodityPrice = realCommodity.Price
            };
            collectionCommodity.SetShoppingOptionParameters(realCommodity.ShoppingOptionParameters);

            var item = await _collectionManager.Add(member, collectionCommodity);
            return new CollectionCommodityDto(item);
        }

        [AbpAuthorize(PermissionNames.Member)]
        public async Task Adds(AddCollectionDto input)
        {
            var member = await GetCurrentMemberAsync();
            foreach(var item in input.Items)
            {
                var realCommodity = await _commodityManager.GetRealCommodity(member, item.CommodityId, item.ShoppingOptionId);
                CollectionCommodity collectionCommodity = new CollectionCommodity
                {
                    CommodityId = item.CommodityId,
                    ShoppingOptionId = item.ShoppingOptionId,
                    CommodityName = realCommodity.CommodityName,
                    CommodityPicture = realCommodity.Picture,
                    CommodityPrice = realCommodity.Price
                };
                collectionCommodity.SetShoppingOptionParameters(realCommodity.ShoppingOptionParameters);

                await _collectionManager.Add(member, collectionCommodity);
            }
        }

        [AbpAuthorize(PermissionNames.Member)]
        public async Task<List<CollectionCommodityDto>> GetAll()
        {
            var member = await GetCurrentMemberAsync();
            var items = await _collectionManager.GetAll(member);
            List<CollectionCommodityDto> collectionCommodityDtos = new List<CollectionCommodityDto>();
            foreach(var item in items)
            {
                collectionCommodityDtos.Add(new CollectionCommodityDto(item));
            }
            return collectionCommodityDtos;
        }

        [AbpAuthorize(PermissionNames.Member)]
        [HttpPost]
        public async Task Remove(DeleteDto input)
        {
            foreach (int id in input.Ids)
            {
                await _collectionManager.Remove((int)AbpSession.UserId, id);
            }
        }

        public async Task Cancel(AddCollectionItemDto input)
        {
            await _collectionManager.Cancel(MemberId, input.CommodityId);
        }

        [AbpAuthorize(PermissionNames.Member)]
        public async Task<SearchResultDto<CollectionCommodityDto>> Search(SearchDto input)
        {
            var query = _collectionCommodityRepository.GetAll().Where(r => r.MemberId == MemberId);
            int totalCount = query.Count();
            var items = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();

            var member = await GetCurrentMemberAsync();
            List<CollectionCommodityDto> collectionCommodityDtos = new List<CollectionCommodityDto>();
            foreach (var item in items)
            {
                collectionCommodityDtos.Add(new CollectionCommodityDto(item));
            }
            return new SearchResultDto<CollectionCommodityDto>
            {
                TotalCount = totalCount,
                Items = collectionCommodityDtos
            };
        }

        public async Task<int> GetCommodityCollectedCount(int commodityId)
        {
            return _collectionCommodityRepository.GetAll().Where(r => r.CommodityId == commodityId).Count();
        }
    }
}
